<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Validation\Validator;
use Hash;
class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        $request = $request -> all();
        // 查询数据并分页
        $data=\DB::table("admin")->where("nickname",'like','%'.$search.'%')->paginate($count); 
           
        // 加载模版
         return view("admin.admin.index",['data'=>$data,'request'=>$request]);

            //with 动态视图赋值
           ;
    //}
}
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.admin.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //Hash::make($data['password']);
       
            // 表单验证的规则
        //dd($data);

        $rules=[
            'nickname' => 'required|unique:admin|regex:/^\w{6,18}$/',
            'password' => 'required|same:repassword|between:6,12',
            'email'=>"required|email",
            'tel'=>"required|regex:/^1[35784]\d{9}$/",
        ];
        // 表单验证的提示信息

        $message=[

            "nickname.required"=>"请输入用户名",
            "nickname.unique"=>"用户名已存在",
            "nickname.regex"=>"用户名不符合规格",
            "password.required"=>"请输入密码",
            "password.same"=>"两次密码不一致",
            "password.between"=>"密码长度不在6-12位之间",
            "email.required"=>"请输入邮箱",
            "email.email"=>"邮箱格式不正确",
            "tel.required"=>"请输入电话",
            "tel.regex"=>"电话格式不正确",

        ];

        // 使用laravel的表单验证
        $this->validate($request,$rules,$message);
        // 开始验证
         $data = $request ->except("_token","repassword"); 
         $data['password']=Hash::make($data['password']);
         $data['createtime']=time();
                // 插入数据库
         $res=\DB::table("admin")->insert($data);

              if($res){
                     return redirect('admin/admin')->with('success','添加成功');
                }else{
                    return back()->with('error','添加失败');
                    }
   }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 查询数据库

        $data=\DB::table("admin")->find($id);
        //分配数据
       return view('admin.admin.edit')->with("data",$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //接收表单数据
        $data=$request ->except("_token","_method");
      
        //表单验证
         $rules=[
            'email'=>"required|email",
            'tel'=>"required|regex:/^1[35784]\d{9}$/",
        ];

        // 表单验证的提示信息
        $message=[
            "email.required"=>"请输入邮箱",
            "email.email"=>"邮箱格式不正确",
            "tel.required"=>"请输入电话",
            "tel.regex"=>"电话格式不正确",
        ];

         $this->validate($request,$rules,$message);
      
         //操作数据库
         $res=\DB::table("admin")->where('id',$id)->update($data);
         
         if($res){
                     return redirect('admin/admin')->with('success','修改成功');
                }else{
                    return back()->with('error','修改失败');
                     }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        if (\DB::table("admin")->delete($id)) {
            return "1";
        }else{
            return 0;
        }
    }



     /**
     * 修改状态的方法
     *
     * @param  
     * @return \Illuminate\Http\Response
     */

    public function ajaxStatu(Request $request)
    {

        $arr=$request->except('_token');
    // dd($arr);
        if (\DB::update("update admin set statu= $arr[statu] where id=$arr[id]")) {
            # code...
            echo 1;
        }else{
            echo 0;
         }
    }


     /**
     * 修改权限的方法
     *
     * @param  
     * @return \Illuminate\Http\Response
     */

    public function ajaxAuthority(Request $request)
    {

        $arr=$request->except('_token');
    // dd($arr);
        if (\DB::update("update admin set authority= $arr[authority] where id=$arr[id]")) {
            # code...
            echo 1;
        }else{
            echo 0;
        }
    }

    /**
     * 修改管理员密码页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function chang($id)
    {   
        //跳转页面
       return view('admin.admin.change')->with('id',$id);
    
    }
    
    /**
     * 修改管理员密码
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function changPwd(Request $request)
    {   
         
         //获取表单数据  

            $id=$request->input('id');
            //与数据库密码比对
            $oldpassword = $request->input('oldpassword');
            //新密码
            $newpassword = $request->input('newpassword');                                        
            $repassword = $request->input('repassword');

        //验证规则

         $rules=[
                    'newpassword' => 'required|same:repassword|between:6,12',      
                    'oldpassword' => 'required|between:6,12',      
                ];


        // 表单验证的提示信息

        $message=[

            "newpassword.required"=>"请输入新密码",
            "newpassword.same"=>"两次密码不一致",
            "newpassword.between"=>"密码长度不在6-12位之间",
            "oldpassword.between"=>"密码长度不在6-12位之间",
            "oldpassword.required"=>"请输入密码",
        ];


        //表单验证方法

        $this->validate($request,$rules,$message);

        //查询数据库
        $res = \DB::table('admin')->where('id',$id)->select('password')->first();
               

            //验证是否和数据库匹配

            if(Hash::check($oldpassword,$res['password'])){
                

                //新密码加密
                $arr['password']=Hash::make($newpassword);

                //执行修改操作
                $res = \DB::table('admin')->where('id',$id)->update($arr);
                
                if($res){

                     return redirect('admin/admin')->with('success','修改成功');

                }else{

                    return back()->with('error','修改失败');
                }
                    
            }else{
                return back()->with('error','修改失败');
            }

    }











}
